using System;

using LocalBoard.Domain;
using LocalBoard.Persistence;

namespace LocalBoard.Controller
{
	/// <summary>
	/// Summary description for ClubSecurity.
	/// </summary>
	public class ClubSecurity
	{

		private ClubController _currentClubController;
		private IAccessorFactory _accessorFactory;

		public ClubSecurity(ClubController clubController, IAccessorFactory accessorFactory)
		{
			_currentClubController = clubController;
			_accessorFactory = accessorFactory;
		}

		public bool CanUserCreateBoard(Profile user)
		{
			if(user.IsInRole(UserRole.ADMIN_ROLE_NAME))
				return true;
			return _currentClubController.IsUserInClubRole(user.Id, UserRole.CLUB_MANAGER_ROLE_NAME);
		}

		public bool CanUserUpdateBoard(Profile user)
		{
			if(user.IsInRole(UserRole.ADMIN_ROLE_NAME))
				return true;
			return _currentClubController.IsUserInClubRole(user.Id, UserRole.CLUB_MANAGER_ROLE_NAME);
		}

		public bool CanUserDeleteBoard(Profile user)
		{
			if(user.IsInRole(UserRole.ADMIN_ROLE_NAME))
				return true;
			return _currentClubController.IsUserInClubRole(user.Id, UserRole.CLUB_MANAGER_ROLE_NAME);
		}

		public bool CanUserManageClub(Profile user)
		{
			if(user.IsInRole(UserRole.ADMIN_ROLE_NAME))
				return true;
			return _currentClubController.IsUserInClubRole(user.Id, UserRole.CLUB_MANAGER_ROLE_NAME);
		}

		public bool CanUserAccessClub(Profile user)
		{
			if(user.IsInRole(UserRole.ADMIN_ROLE_NAME))
				return true;

			AdminController controller = new AdminController(_accessorFactory);
			Club target = controller.GetClub(_currentClubController.ClubId);
			
			if(target.ClubType == ClubType.Public)
				return true;
			if((target.ClubType == ClubType.Private || target.ClubType == ClubType.Hidden)
				&& _currentClubController.IsUserInTheClub(user.Id))
				return true;

			return false;
		}

		public bool CanUserSeeClubListing(Profile user)
		{
			if(user.IsInRole(UserRole.ADMIN_ROLE_NAME))
				return true;

			AdminController controller = new AdminController(_accessorFactory);
			Club target = controller.GetClub(_currentClubController.ClubId);
			
			if(target.ClubType == ClubType.Hidden
				&& _currentClubController.IsUserInTheClub(user.Id))
				return true;
			if(target.ClubType == ClubType.Public
				|| target.ClubType == ClubType.Private)
				return true;
			
			return false;
		}
	}
}
